package com.fortune.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fortune.dao.DistributorOrderMgtDao;
import com.fortune.dto.in.distributorOrder.DistributorOrderAddDto;
import com.fortune.dto.in.distributorOrder.DistributorOrderDetailAddDto;
import com.fortune.dto.in.distributorOrder.DistributorOrderEditDto;
import com.fortune.dto.in.distributorOrder.DistributorOrderListDto;
import com.fortune.entity.MProductInfo;
import com.fortune.entity.MUserInfo;
import com.fortune.service.DistributorOrderMgtService;
import com.fortune.vo.distributorOrderMgt.DistributorOrder;
import com.fortune.vo.distributorOrderMgt.DistributorOrderDetail;

@Service
public class DistributorOrderMgtServiceImpl implements DistributorOrderMgtService {
	@Autowired
	private DistributorOrderMgtDao distributorOrderMgtDao;
	@Override
	public List<DistributorOrder> list(MUserInfo mUserInfo,DistributorOrderListDto distributorOrderMgtListDto) {
		List<DistributorOrder> list = distributorOrderMgtDao.list(mUserInfo,distributorOrderMgtListDto);
		return list;
	}
	
	@Override
	public int count(MUserInfo mUserInfo,DistributorOrderListDto distributorOrderMgtListDto) {
		int count = distributorOrderMgtDao.count(mUserInfo,distributorOrderMgtListDto);
		return count;
	}
	
	@Override
	public DistributorOrderDetail detail(String mId, String dId, String orderId) {
		DistributorOrderDetail detail = distributorOrderMgtDao.detail(mId, dId, orderId);
		return detail;
	}
	@Override
	@Transactional
	public int add(DistributorOrderAddDto addDto,String mId,String createUser) {
		String id = UUID.randomUUID().toString().replace("-","");
		List<DistributorOrderDetailAddDto> list = addDto.getDetails();
		BigDecimal pTotalPrice = null;
		BigDecimal dTotalPrice = null;
		for(DistributorOrderDetailAddDto distributorOrderDetailAddDto: list){
			String productId = distributorOrderDetailAddDto.getProductId();
			MProductInfo mProductInfo = distributorOrderMgtDao.dProductInfo(mId, productId);
			if(mProductInfo==null)
				return 2;
			BigDecimal price = mProductInfo.getWholesalePrice();
			BigDecimal boxPrice = mProductInfo.getBoxPrice();
			int num = distributorOrderDetailAddDto.getOrderNum();
			BigDecimal totalPrice = price.multiply(new BigDecimal(num));
			BigDecimal boxTotalPrice = boxPrice.multiply(new BigDecimal(num));
			if(pTotalPrice==null){
				pTotalPrice = totalPrice;
			}else {
				pTotalPrice = pTotalPrice.add(totalPrice);
			}
			if(dTotalPrice==null){
				dTotalPrice = boxTotalPrice;
			}else {
				dTotalPrice = dTotalPrice.add(boxTotalPrice);
			}
			distributorOrderMgtDao.addDetail(distributorOrderDetailAddDto, mId, productId, id, totalPrice, boxTotalPrice, boxTotalPrice);
		}
		int add = distributorOrderMgtDao.add(addDto, mId,createUser,id,pTotalPrice,dTotalPrice);
		return add;
	}
	@Override
	@Transactional
	public int edit(DistributorOrderEditDto editDto,String mId,String createUser) {
		String id = editDto.getOrderId();
		String dId = editDto.getDistributorId();
		int delete = distributorOrderMgtDao.deleteDetal(mId, dId, id);
		List<DistributorOrderDetailAddDto> list = editDto.getDetails();
		BigDecimal pTotalPrice = null;
		BigDecimal dTotalPrice = null;
		for(DistributorOrderDetailAddDto distributorOrderDetailAddDto: list){
			String productId = distributorOrderDetailAddDto.getProductId();
			MProductInfo mProductInfo = distributorOrderMgtDao.dProductInfo(mId, productId);
			BigDecimal price = mProductInfo.getWholesalePrice();
			BigDecimal boxPrice = mProductInfo.getBoxPrice();
			int num = distributorOrderDetailAddDto.getOrderNum();
			BigDecimal totalPrice = price.multiply(new BigDecimal(num));
			BigDecimal boxTotalPrice = boxPrice.multiply(new BigDecimal(num));
			if(pTotalPrice==null){
				pTotalPrice = totalPrice;
			}else {
				pTotalPrice = pTotalPrice.add(totalPrice);
			}
			if(dTotalPrice==null){
				dTotalPrice = boxTotalPrice;
			}else {
				dTotalPrice = dTotalPrice.add(boxTotalPrice);
			}
			int add = distributorOrderMgtDao.addDetail(distributorOrderDetailAddDto, mId, productId, id, totalPrice, boxTotalPrice, boxTotalPrice);
		}
		int result = distributorOrderMgtDao.edit(editDto, mId, createUser, pTotalPrice, dTotalPrice);
		return result;
	}
	@Override
	public int delete(String mId, String dId, String orderId) {
		int result = distributorOrderMgtDao.delete(mId, dId, orderId);
		return result;
	}
	@Override
	public int approve(String mId, String dId, String orderId,String shipStore,String createUser) {
		int result = distributorOrderMgtDao.approve(mId, dId, orderId,shipStore,createUser);
		return result;
	}

}
